package com.snail.jj.androiddemos.bases;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.snail.jj.androiddemos.R;

/**
 * Created by snail on 2017/4/26.
 */

public class ComponentsActivity extends BaseActivity {

    private ListView listView;
    private String[] dataSource = {
            "Activity",
            "Service",
            "BroadcastReceiver",
            "ContentProvider"
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_components);
        initView();
    }

    public void initView() {
        setTitleBarTitle("四大组件");
        setRightViewVisiable(View.INVISIBLE);
        listView = (ListView) findViewById(R.id.id_components_listView);
        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,dataSource);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String className = "com.snail.jj.androiddemos.components." + dataSource[position] + "Activity";
                try {
                    Class<?> clazz = Class.forName(className);
                    BaseActivity activity = (BaseActivity) clazz.newInstance();
                    Intent intent = new Intent(ComponentsActivity.this,activity.getClass());
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
